package com.code.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.springboot.common.CustomException;
import com.code.springboot.entity.Category;
import com.code.springboot.entity.Dish;
import com.code.springboot.entity.Setmeal;
import com.code.springboot.mapper.CategoryMapper;
import com.code.springboot.service.CategoryService;
import com.code.springboot.service.DishService;
import com.code.springboot.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: springboot
 * @create: 2022-08-10 10:16
 **/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;



    @Autowired
    private SetmealService setmealService;

    //根据id删除分类，删除前需判断
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);

        if(count1 > 0){
            //查询分类是否关联了菜品，关联则抛出异常
            throw new CustomException("当前分类关联了菜品，不能删除");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);

        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            //查询分类是否关联了套餐，关联则抛出异常
        }


        //删除分类
        super.removeById(id);

    }
}
